package design.graduation.novelsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import design.graduation.novelsystem.mapper.SystemNoticeMapper;
import design.graduation.novelsystem.model.domian.SystemNotice;
import design.graduation.novelsystem.model.dto.ActiveAdmin;
import design.graduation.novelsystem.model.dto.DataGridView;
import design.graduation.novelsystem.model.vo.SystemNoticeVo;
import design.graduation.novelsystem.service.SystemNoticeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author: 洪少尉
 * @Date: 2020/4/15 16:13
 * @Description: 
 */
@Service
public class SystemNoticeServiceImpl extends ServiceImpl<SystemNoticeMapper, SystemNotice> implements SystemNoticeService{

	/**
	 * 全查询+条件查询
	 *
	 * @param systemNoticeVo 查询条件
	 * @return DataGridView
	 */
	@Override
	public DataGridView queryAllSystemNotice(SystemNoticeVo systemNoticeVo) {
		IPage<SystemNotice> page=new Page<>(systemNoticeVo.getPage(),systemNoticeVo.getLimit());
		QueryWrapper<SystemNotice> queryWrapper=new QueryWrapper<>();
		queryWrapper.like(StringUtils.isNotBlank(systemNoticeVo.getTitle()),SystemNotice.COL_TITLE,systemNoticeVo.getTitle())
				.like(StringUtils.isNotBlank(systemNoticeVo.getPublisher()),SystemNotice.COL_PUBLISHER,systemNoticeVo.getPublisher())
				.ge(systemNoticeVo.getStartTime()!=null,SystemNotice.COL_CREATE_TIME,systemNoticeVo.getStartTime())
				.le(systemNoticeVo.getEndTime()!=null,SystemNotice.COL_EDIT_TIME,systemNoticeVo.getEndTime())
				.orderByDesc(SystemNotice.COL_EDIT_TIME);
		this.baseMapper.selectPage(page,queryWrapper);
		return new DataGridView(page.getTotal(),page.getRecords());
	}

	/**
	 * 保存系统公告
	 *
	 * @param systemNotice 系统公告信息
	 * @return boolean
	 */
	@Override
	public boolean saveSystemNotice(SystemNotice systemNotice) {
		ActiveAdmin activeAdmin = (ActiveAdmin) SecurityUtils.getSubject().getPrincipal();
		systemNotice.setPublisher(activeAdmin.getAdmin().getName())
				.setCreateTime(new Date())
				.setEditTime(new Date());
		return this.save(systemNotice);
	}
}
